Url Shortening App in Flask
URL Shortening Application in Flask
๐ ๏ธ Description
This project is about developing a url shortening application in Flask and MongoDB. User will paste their long URLs in this application and will get a shortened url, which will redirect to the same long url once used in a browser.
โ๏ธ Languages or Frameworks Used
- Flask, MongoDB
- HTML, CSS, Bootstrap
๐ How to run
Install all the requirements
Runpip install -r requirements.txtto install all the requirements.MongoDB Setup for Project
- Download monogdb from the official website and setup in your local system for testing.
- Once it is setup locally, try creating documents and collections in mongodb to familiarize yourself with it.
- You can also download the
MongoDB Compass, which is the GUI version of Mongo Shell. - Once all the local testing is done, you can create a free cloud version of MongoDB in MongoDB Atlas and get the following credentials from the dashboard of atlas:
export MONGO_URI=YOUR_MONGO_URI export MONGO_USERNAME=YOUR_MONGO_USERNAME export MONGO_PASSWORD=YOUR_MONGO_PASSWORDSetup Environment for the project
- Now create a
.envfile in your project dreictory and include the following parameters as it is :-
export ENVIRONMENT=local | production (choose on the basis of local or production environment) export APP_SECRET=YOUR_APP_SECRET export APP_URL=YOUR_APP_URL (the short url) export MONGO_URI=YOUR_MONGO_URI export MONGO_USERNAME=YOUR_MONGO_USERNAME export MONGO_PASSWORD=YOUR_MONGO_PASSWORD export DB_NAME=YOUR_DATABASE_NAME- Now create a
Now Just, Run the project
- To the run the project, go to the
bashterminal of VSCode or any other code editor and run./start_server.sh. - You donโt have to care about setting
.envthen yourself then.
- To the run the project, go to the
๐บ Demo
- Main screen of the application.

- Paste you long URL in the input.

- Click on Shorten and copy the
short urlto clipboard
Source Code: helpers.py
import string
import random
import re
def random_string_generator(N):
res = ''.join(random.choices(string.ascii_lowercase +
string.digits, k=N))
return str(res)
def random_string_generator_only_alpha(N):
res = ''.join(random.choices(string.ascii_lowercase +
string.ascii_uppercase, k=N))
return str(res)
def is_valid_url(url):
# Define a regular expression pattern to match URLs
url_pattern = re.compile(
r'^(https?|ftp)://' # Match the scheme (http, https, or ftp)
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' # Match domain
r'localhost|' # Match "localhost"
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # Match IP address
r'(?::\d+)?' # Match optional port number
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
return bool(url_pattern.match(url))